<template>
  <div class="login_container">
    <!-- 登录盒子  -->
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt>
      </div>
      <div class="title">欢迎使用建设工程智慧管理平台</div>
      <!-- 登录表单 -->
      <el-form :model="loginForm" ref="loginForm" :rules="rules" label-width="0px" class="login_form" >
        <!-- 账号 -->
        <el-form-item prop="phone">
          <el-input @keyup.enter="login('loginForm')" v-model="loginForm.phone" prefix-icon="el-icon-user" placeholder="请输入账号"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password" style="margin-bottom:15px">
          <el-input @keyup.enter="login('loginForm')" type="password" v-model="loginForm.password" prefix-icon="el-icon-lock" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-checkbox v-model="checked" style="font-size: 12px; color: #888;">记住我</el-checkbox>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login('loginForm')">登录</el-button>
          <el-button type="info" @click="resetLoginForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="link">
        <router-link class="forget" to="/">忘记密码？</router-link>
        <router-link class="register" to="/register">注册账号</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    const validatePhone = (rule, value, callback) =>{
      const reg =/^[1][3,4,5,7,8][0-9]{9}$/;
      if (value === "") {
        callback(new Error("请输入手机号码"));
      } else {
        if (!reg.test(value) && value !== "") {
          callback(new Error("请输入正确的手机号码"));          
        }else{
          callback();
        }
      }
    };
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      checked: true,
      // 数据绑定
      loginForm: {
        phone: "",
        password: ""
      },
      // 表单验证规则
      rules: {
        phone: [{ validator: validatePhone, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }]
      }
    };
  },
  methods: {
    resetLoginForm(form) {
      // this=>当前组件对象，其中的属性$refs包含了设置的表单ref
      this.$refs[form].resetFields();
    },
    login(form) {
      this.$refs[form].validate(async valid => {
        if (!valid) { return; }
        // 发送登录请求
        const { data: res } = await this.$http.get("user/login", {
          params: {
            phone: this.loginForm.phone,
            password: this.$md5(this.loginForm.password),
            autoLogin: this.checked
          }
        });
        if (res.code != 200) { return this.$message.error(res.msg); }
        if (res.code == 200){ this.$message.success("登录成功");
          if (this.checked) {
            this.$store.commit("$_setLocalToken", res.data.tokenValue);
            this.$store.commit("autoLogin", true);
          } else {this.$store.commit("autoLogin", false);}
          this.$store.commit("$_setToken", res.data.tokenValue);
          // 导航至/main  通过编程式导航跳转到后台主页
          this.$router.push("/main");
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #373d41;
  height: 100%;
}
.login_box {
  width: 45rem;
  height: 35rem;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .title {
    width: 100%;
    height: 30px;
    position: absolute;
    text-align: center;
    top: 90px;
    font-size: 20px;
    color: #373d41;
  }
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .link {
    width: 100%;
    position: absolute;
    bottom: 20px;
    font-size: 14px;
    a {
      color: #888;
      text-decoration: none;
    }
    a:hover {
      color: rgb(60, 150, 250);
      text-decoration: underline;
    }
    .register {
      float: right;
      margin-right: 20px;
    }
    .forget {
      float: left;
      margin-left: 20px;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  text-align: center;
}
</style>
